package edu.uwm.cs552;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * A class to store information about cities.
 * This class is immutable and instances are not registered.
 */
public class City implements Iterable<Good> {

	private final String name;
	private final List<Good> goods = new ArrayList<Good>();
	
	/**
	 * Create city information for a named city.
	 * The name cannot include a comma (to allow comma separation).
	 * @param n name of city, must not be null or include a comma
	 */
	public City(String n) {
		if (n == null || n.indexOf(',') >= 0) {
			throw new IllegalArgumentException("name of city cannot include a comma");
		}
		name = n;
	}
	
	public String getName() {
		return name;
	}
	
	public void addGood(Good g) {
		goods.add(g);
	}
	
	/**
	 * Return true if this good is one of the ones normally
	 * provided by this city.  It does not check whether the good
	 * is available.
	 * @param g good to check, must not be null
	 * @return true if this city provides this good.
	 */
	public boolean hasGood(Good g) {
		if (g == null) throw new IllegalArgumentException("good may not be null");
		for (Good has : goods) {
			if (has == g) return true;
		}
		return false;
	}
	
	@Override
	public String toString() {
		return name;
	}
	
	public String toWrite() {
		String s = name;
		for (Good g : goods) 
			s = s + "," + g.toString();
		return s;
	}
	
	public Iterator<Good> iterator() {
		return goods.iterator();
	}
	
	private Font lastFont = null;
	private double lastScale = 0.0;
	
	public void draw(Graphics g, HexCoordinate h, double scale) {
		if (scale != lastScale) {
			lastFont = new Font("Sans",Font.PLAIN,(int)(scale/2));
			lastScale = scale;
		}
		g.setFont(lastFont);
		g.setColor(Color.BLACK);
		Point p = h.toPoint(scale);
		g.drawString(name, p.x + 5, p.y+(int)(scale/4));
	}
}
